package com.zyl.util;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.Reader;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Environment;

/**
 * 文件工具类
 * 
 * @author zyl 2010-1-6下午02:14:41
 */
public class FileUtil {
	public static final int MAX_FILE_BUFFER_SIZE = 4 * 1024;

	private FileUtil() {
	}
/*
	public static Reader fileToStream(String fileName)
			throws FileNotFoundException {
		Reader inputStream = null;
		if (fileName != null) {
			inputStream = new BufferedReader(new InputStreamReader(
					new FileInputStream(fileName), Charset.forName("GBK")),
					MAX_FILE_BUFFER_SIZE);
		}

		return inputStream;
	}

	private static String path = Environment.getExternalStorageDirectory()
			.getPath() + "//agnote.txt";

	public static void createFile() throws IOException {
		File file = new File(path);

		if (!file.exists()) {
			file.createNewFile();

		}

	}

	public static String read() throws IOException {
		createFile();
		BufferedReader br = new BufferedReader(new FileReader(path));

		String str = "";

		String r = br.readLine();

		while (r != null) {
			str += r+"\n";
			r = br.readLine();

		}
		return str;
	}

	public static void editTxt(String txt) throws IOException {
		createFile();
		FileWriter fileWriter = new FileWriter(path);
		fileWriter.write(txt);
		fileWriter.flush();
		fileWriter.close();

	}
	 public static boolean write (String newStr) throws IOException {

	         // 先读取原有文件内容，然后进行写入操作

	         boolean flag = false;

	         String filein = newStr + "\r\n";

	         String temp = "";

	

	         FileInputStream fis = null;

	         InputStreamReader isr = null;

	         BufferedReader br = null;

	

	         FileOutputStream fos = null;

	         PrintWriter pw = null;

	         try {

	             // 文件路径
	        	 createFile();
	             File file = new File(path);

	             // 将文件读入输入流

	             fis = new FileInputStream(file);

	             isr = new InputStreamReader(fis);

	             br = new BufferedReader(isr);

	             StringBuffer buf = new StringBuffer();
	             // 保存该文件原有的内容
	             buf.append(filein);
	             while ( (temp = br.readLine()) != null ) {

	                 buf = buf.append(temp);
	                 buf = buf.append(System.getProperty("line.separator"));

	             }

	           

	

	             fos = new FileOutputStream(file);

	             pw = new PrintWriter(fos);

	             pw.write(buf.toString().toCharArray());

	             pw.flush();

	             flag = true;

	         } catch (IOException e1) {

	             throw e1;

	         } finally {

	             if (pw != null) {

	                 pw.close();

	             }

	             if (fos != null) {

	                 fos.close();

	             }

	             if (br != null) {

	                 br.close();

	             }

	             if (isr != null) {

	                 isr.close();

	             }

	             if (fis != null) {

	                 fis.close();

	             }

	         }

	         return flag;


	     }

	

*/
}
